<template>
  <div>
    <el-card style="margin: 20px 0">
      <CategoryList
        @getCategoryId="getCategoryId"
        :show="scene !== 0"
        @clearAttrList="clearAttrList"
      ></CategoryList>
    </el-card>
    <el-card>
      <!-- spu列表 -->
      <div v-show="scene === 0">
        <!-- 左上角部分--添加SPU按钮 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <!-- 中间部分--表格 -->
        <el-table style="width: 100%" border :data="records">
          <el-table-column
            type="index"
            align="center"
            label="序号"
            width="80px"
          >
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <!-- 添加 -->
              <SelectOperation
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></SelectOperation>
              <!-- 修改 -->
              <SelectOperation
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="updateSpu(row)"
              ></SelectOperation>
              <!-- 查看 -->
              <SelectOperation
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu的sku列表"
                @click="handler(row)"
              ></SelectOperation>
              <!-- 删除 -->
              <el-popconfirm
                :title="`确定${row.spuName}删除吗？`"
                @onConfirm="deleteData(row)"
              >
                <SelectOperation
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                  slot="reference"
                ></SelectOperation>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 最下部分--分页器 -->
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-size="limit"
          :page-sizes="[3, 5, 10]"
          layout="prev, pager, next,jumper,->,sizes,total"
          :total="total"
          @current-change="getSpuList"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
      <!-- SpuForm添加与编辑spu组件 -->
      <SpuForm
        v-show="scene === 1"
        @changeScene="changeScene"
        ref="spu"
      ></SpuForm>
      <!-- SkuForm添加sku组件 -->
      <SkuForm
        v-show="scene === 2"
        @changeScenes="changeScenes"
        ref="sku"
      ></SkuForm>
      <!-- 查看sku列表对话框 -->
      <el-dialog
        :title="`${spu.spuName}的sku列表`"
        :visible.sync="dialogTableVisible"
        :before-close="clearData"
      >
        <el-table
          :data="skuList"
          style="width: 100%"
          border
          v-loading="loading"
        >
          <el-table-column prop="skuName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="price" label="价格(元)" width="width">
          </el-table-column>
          <el-table-column prop="weight" label="重量(KG)" width="width">
          </el-table-column>
          <el-table-column label="默认图片" width="width">
            <template slot-scope="{ row }">
              <img
                :src="row.skuDefaultImg"
                alt=""
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";

export default {
  name: "Spu",
  components: { SpuForm, SkuForm },
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      page: 1,
      limit: 3,
      total: 0,
      scene: 0,
      records: [],
      dialogTableVisible: false,
      skuList: [],
      spu: "",
      loading: true,
    };
  },
  methods: {
    //获取spu信息
    async getSpuList(pages = 1) {
      this.page = pages;
      const { page, limit, category3Id } = this;
      let result = await this.$API.spu.reqSpu(page, limit, category3Id);
      if (result.code === 200) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    //存储id以及发请求获取属性列表
    getCategoryId({ categoryId, level }) {
      if (level === 1) {
        this.category1Id = categoryId;
        this.category2Id = "";
        this.category3Id = "";
      } else if (level === 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
      } else {
        this.category3Id = categoryId;
        //发请求
        this.getSpuList();
      }
    },
    //清除spu信息列表
    clearAttrList() {
      this.records = [];
    },
    //当每一页展示数据发生变化时调用
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    //点击添加spu按钮显示SpuForm组件
    addSpu() {
      this.scene = 1;
      this.$refs.spu.getBrandAndSale(this.category3Id);
    },
    //点击table操作中修改按钮显示SpuForm组件
    updateSpu(row) {
      this.scene = 1;
      this.$refs.spu.initSpuData(row);
    },
    //点击table操作中添加按钮显示SkuForm组件
    addSku(row) {
      this.scene = 2;
      this.$refs.sku.getAddSkuList(row, this.category1Id, this.category2Id);
    },
    //在SpuForm组件中点击取消按钮隐藏SpuForm组件
    changeScene(flag) {
      this.scene = 0;
      if (flag == "修改") {
        this.getSpuList(this.page);
      } else {
        this.getSpuList();
      }
    },
    //删除指定spu回调
    async deleteData(row) {
      let result = await this.$API.spu.reqDeleteData(row.id);
      if (result.code === 200) {
        this.$message({ type: "success", message: `删除"${row.spuName}"成功` });
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
    //触发SkuForm取消按钮时回调
    changeScenes(scene) {
      this.scene = scene;
    },
    //点击查看sku列表  获取sku列表数据
    async handler(row) {
      this.spu = row;
      this.dialogTableVisible = true;
      let result = await this.$API.spu.reqSkuList(row.id);
      if (result.code == 200) {
        this.skuList = result.data;
        this.loading = false;
      }
    },
    //对话框关闭时  清除数据
    clearData(done) {
      this.loading = true;
      this.skuList = [];
      done();
    },
  },
};
</script>

<style>
</style>